namespace Microsoft.VisualStudio.EnterpriseTools.OperationDesigner
{
    using Microsoft.VisualStudio.VirtualTreeGrid;
    using System;
    using System.Security.Permissions;
    using System.Windows.Forms;

    public class OperationDesignerInPlaceEdit : VirtualTreeInPlaceEditControl
    {
        public OperationDesignerInPlaceEdit()
        {
            base.BorderStyle = BorderStyle.None;
            this.AutoSize = false;
            this.Flags &= ~VirtualTreeInPlaceControlFlags.SizeToText;
        }

        protected override AccessibleObject CreateAccessibilityInstance()
        {
            base.AccessibleName = GetAccessibleObjectName(base.Parent as VirtualTreeControl);
            return base.CreateAccessibilityInstance();
        }

        internal static string GetAccessibleObjectName(VirtualTreeControl parent)
        {
            VirtualTreeControl control = parent;
            if (control != null)
            {
                VirtualTreeColumnHeader[] columnHeaders = control.GetColumnHeaders();
                if (columnHeaders != null)
                {
                    int index = control.CurrentColumn;
                    if ((index >= 0) && (index < columnHeaders.Length))
                    {
                        return columnHeaders[index].Text;
                    }
                }
            }
            return string.Empty;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (!e.Handled)
            {
                OperationDesignerTreeControl control = base.Parent as OperationDesignerTreeControl;
                if (control != null)
                {
                    e.Handled = ProcessKeyReturn.NotHandled != control.ProcessKeyDown(e);
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (!e.Handled)
            {
                OperationDesignerTreeControl control = base.Parent as OperationDesignerTreeControl;
                if (control != null)
                {
                    e.Handled = ProcessKeyReturn.NotHandled != control.ProcessKeyPress(e.KeyChar, Control.ModifierKeys);
                }
            }
        }

        protected override void OnLostFocus(EventArgs e)
        {
            OperationDesignerTreeControl control = base.Parent as OperationDesignerTreeControl;
            base.OnLostFocus(e);
            if ((control != null) && control.InsertMode)
            {
                control.EndInsert();
            }
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode), SecurityPermission(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            Keys keys = keyData & Keys.KeyCode;
            if (keys != Keys.Delete)
            {
                if (keys == Keys.A)
                {
                    if ((((keyData & Keys.Control) != Keys.None) && ((keyData & Keys.Shift) == Keys.None)) && ((keyData & Keys.Alt) == Keys.None))
                    {
                        base.SelectAll();
                        return true;
                    }
                }
                else if (((keys == Keys.Z) && ((keyData & Keys.Control) != Keys.None)) && (((keyData & Keys.Shift) == Keys.None) && ((keyData & Keys.Alt) == Keys.None)))
                {
                    base.Undo();
                    return true;
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
            Microsoft.VisualStudio.EnterpriseTools.OperationDesigner.NativeMethods.SendMessage(base.Handle, msg.Msg, msg.WParam.ToInt32(), msg.LParam.ToInt32());
            return true;
        }
    }
}

